<!DOCTYPE html>
<!--

// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<title></title>
<script src="../../../third_party/closure-library/closure/goog/base.js"></script>
<script>
goog.require('goog.testing.jsunit');
</script>
<script src="../../../src/traceur.js"></script>
</head>
<body>
<script type="text/traceur">

function testDefaultParameters() {
  function f(a = 1, b = 2) {
    return a + b;
  }

  assertEquals(0, f.length);
  assertEquals(3, f());
  assertEquals(6, f(4));
  assertEquals(24, f(8, 16));

  function g(a, b = a) {
    return a + b;
  }

  assertEquals(1, g.length);
  assertEquals(4, g(2));
  assertEquals(5, g(2, 3));

  function C(obj = this) {
    this.obj = obj;
  }

  assertEquals(0, C.length);

  var c = new C;
  assertEquals(c, c.obj);

  var c2 = new C(42);
  assertEquals(42, c2.obj);
}

</script>
<script>

function evalScript(scriptElement) {
  var source;
  var name;
  var url = scriptElement.src || window.location.href;
  if (scriptElement.src) {
    name = scriptElement.src;
    var xhr = new XMLHttpRequest();
    // Synchronous load but these are already loaded when we get here so it
    // should be fast.
    xhr.open('GET', url, false);
    xhr.send();
    source = xhr.responseText;
  } else {
    name = 'inline-script';
    source = scriptElement.textContent;
  }

  var errors = new traceur.util.ErrorReporter();
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var tree = traceur.codegeneration.Compiler.compileFile(errors,
                                                         sourceFile,
                                                         url);
  var code = traceur.outputgeneration.TreeWriter.write(tree);
  traceur.strictGlobalEval(code);
  assertFalse(errors.hadError());
}

function evalAllScripts() {
  var scripts = document.querySelectorAll('script[type="text/traceur"]');
  Array.prototype.forEach.call(scripts, evalScript);
}

function testInvalidDefaultParams() {
  var name = 'test';
  var source =
     'function invalidParam(noDefault, defaultParam = 1, noDefaultAgain) {\n' +
     '  // Should fail to parse since default param is followed by a non\n' +
     '  // default param.\n' +
     '}\n';

  var errors = [];

  var errorReporter = {
    reportError: function(position, message) {
      errors.push({position: String(position), message: message});
    },
    hadError: function() {
      return errors.length > 0;
    }
  };

  var sourceFile = new traceur.syntax.SourceFile(name, source);
  traceur.codegeneration.Compiler.compileFile(errorReporter, sourceFile,
                                              window.location.href);

  assertEquals('test(1, 66)', errors[0].position);
  assertEquals('\'=\' expected', errors[0].message);
}

evalAllScripts();

</script>
</body>
</html>
